package com.itouchtv.yang.core.tools;

import com.itouchtv.yang.core.driver.CommDriver;
import org.openqa.selenium.Keys;
import org.openqa.selenium.interactions.Actions;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.Objects;

public class RobotUpLoad {

    public static void upLoad(String filePath){
        // 指定图片的路径，这里我放桌面上
        filePath = filePath.replace("/","\\");
        StringSelection sel = new StringSelection(filePath);
        // 把图片文件路径复制到剪贴板
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel,null);
        robotKeyAction();
    }

    private static void robotKeyAction(){
        Robot robot = null;
        try {
            robot = new Robot();
            Thread.sleep(1000);
        } catch (AWTException | InterruptedException e) {
            e.printStackTrace();
        }
        Objects.requireNonNull(robot,"robot=null");
        // 按下回车
//        robot.keyPress(KeyEvent.VK_ENTER);

        // 释放回车
//        robot.keyRelease(KeyEvent.VK_ENTER);

        // 按下 CTRL+V

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);

        // 释放 CTRL+V
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_V);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 点击回车 Enter
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    }

    private static void driverKeyAction(){
        Actions action=new Actions(CommDriver.driver.get().getWebDriver());
//        action.sendKeys(Keys.ENTER);
        action.keyDown(Keys.LEFT_CONTROL);
        action.keyDown("v");
        action.keyUp(Keys.LEFT_CONTROL);
        action.keyUp("v");
        action.keyDown(Keys.ENTER);
        action.keyUp(Keys.ENTER);
    }
}
